package com.zhanghe.study.mapreduce.friend;

import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.Reducer;

import java.io.IOException;


/**
 * @author zh
 * @date 2021/4/2 15:00
 */
public class FriendReducer extends Reducer<Text, Text,Text,Text> {

    Text v = new Text();
    // 先列出来某个人都是谁的好友
    @Override
    protected void reduce(Text key, Iterable<Text> values, Context context) throws IOException, InterruptedException {
        StringBuilder sb = new StringBuilder();
        for(Text value : values){
            sb.append(value.toString()).append(",");
        }
        // 去除末尾的逗号
        if(sb.length() > 0 && sb.lastIndexOf(",") == sb.length()-1){
            sb.deleteCharAt(sb.length()-1);
        }
        v.set(sb.toString());
        context.write(key,v);
    }
}
